package com.jk.testtrae11

import android.accessibilityservice.GestureDescription
import android.content.ComponentName
import android.content.Intent
import android.graphics.Color
import android.graphics.Path
import android.os.Bundle
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
import com.blankj.utilcode.util.ScreenUtils
import com.jk.testtrae11.databinding.ActivityMainBinding
import com.jk.testtrae11.databinding.SettingGuideBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.withContext

class MainActivity : AppCompatActivity(), AssistsServiceListener {
    private var isActivityResumed = false

    val viewBind: ActivityMainBinding by lazy {
        ActivityMainBinding.inflate(layoutInflater).apply {
            btnAccessibility.setOnClickListener {
                AssistsCore.openAccessibilitySetting()
                startActivity(Intent(this@MainActivity, SettingGuideActivity::class.java))
            }
            btnFlowWindow.setOnClickListener {
                // 创建一个新的视图作为悬浮窗内容
                // 创建一个新的视图作为悬浮窗内容
                val contentView = LinearLayout(this@MainActivity).apply {
                    orientation = LinearLayout.VERTICAL
                    gravity = android.view.Gravity.CENTER

                    // 添加第一个按钮：回到桌面画圆
                    addView(Button(context).apply {
                        text = "打开微信—>上滑"
                        layoutParams = LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT
                        ).apply {
                            setMargins(0, 0, 0, 20)  // 底部margin 20dp
                        }
                        setOnClickListener {

                            CoroutineWrapper.launch {
                                Intent().apply {
                                    addCategory(Intent.CATEGORY_LAUNCHER)
                                    addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                                    component = ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI")
                                    AssistsService.instance?.startActivity(this)
                                }

                                delay(2000)

                                val screenWidth = ScreenUtils.getScreenWidth()
                                val screenHeight = ScreenUtils.getScreenHeight()
                                val centerX = screenWidth / 2f
                                val centerY = screenHeight / 2f
                                val radius1 = 200f // 第一个圆的半径

                                // 创建第一个圆的路径
                                val path1 = Path()
                                path1.addCircle(centerX, centerY, radius1, Path.Direction.CW)

                                // 创建两个手势描述
                                val stroke1 = GestureDescription.StrokeDescription(path1, 0, 2000)

                                val gestureBuilder = GestureDescription.Builder()
                                gestureBuilder.addStroke(stroke1)

                                // 分发手势
                                AssistsCore.dispatchGesture(gestureBuilder.build())
                            }
                        }
                    })

                    // 添加第二个按钮：回到桌面左划
                    addView(Button(context).apply {
                        text = "回到桌面—>左划"
                        layoutParams = LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT
                        )
                        setOnClickListener {
                            // TODO: 实现回到桌面左划的功能
                        }
                    })
                }
                
                // 创建悬浮窗包装器
                val windowWrapper = AssistsWindowWrapper(
                    contentView,
                    wmLayoutParams = AssistsWindowManager.createLayoutParams().apply {
                        width = (ScreenUtils.getScreenWidth() * 0.8).toInt()
                        height = (ScreenUtils.getScreenHeight() * 0.5).toInt()
                    },
                    onClose = {
                        AssistsWindowManager.removeView(it)
                    }
                ).apply {
                    initialCenter = true  // 设置初始位置为屏幕中心
                    showOption = true    // 显示操作按钮（移动、缩放、关闭）
                }
                
                // 添加悬浮窗
                AssistsWindowManager.add(windowWrapper)
            }
            btnStartApp.setOnClickListener {
                Intent().apply {
                    addCategory(Intent.CATEGORY_LAUNCHER)
                    addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                    component = ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI")
                    AssistsService.instance?.startActivity(this)
                }
            }

            btnNotification.setOnClickListener {
//                OverlayAdvanced.onClose = {
//                    OverlayAdvanced.hide()
//                }
//                if (OverlayAdvanced.showed) {
//                    OverlayAdvanced.hide()
//                } else {
//                    OverlayAdvanced.show()
//                }
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(viewBind.root)
        AssistsService.listeners.add(this)
    }

    override fun onResume() {
        super.onResume()
        isActivityResumed = true
        checkServiceEnable()
    }

    override fun onPause() {
        super.onPause()
        isActivityResumed = false
    }

    private fun checkServiceEnable() {
        if (!isActivityResumed) return
        if (AssistsCore.isAccessibilityServiceEnabled()) {
            viewBind.btnAccessibility.isVisible = false
            viewBind.btnFlowWindow.isVisible = true
            viewBind.btnStartApp.isVisible = true
            viewBind.btnNotification.isVisible = true
        } else {
            viewBind.btnAccessibility.isVisible = true
            viewBind.btnAccessibility.text = "获取无障碍服务能力"
            viewBind.btnFlowWindow.isVisible = false
            viewBind.btnStartApp.isVisible = false
            viewBind.btnNotification.isVisible = false
        }
    }

}